package com.doxiaopu.students.organization.controller;

import com.doxiaopu.students.component.controller.BaseController;
import com.doxiaopu.students.component.result.ResponseResult;
import com.doxiaopu.students.organization.entity.OrderRevenueSettings;
import com.doxiaopu.students.organization.entity.Organization;
import com.doxiaopu.students.organization.service.IOrderRevenueSettingsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
@Api(tags = "订单收入设置信息")
@RestController
@CrossOrigin
@RequestMapping("/orderRevenueSettings")
public class OrderRevenueSettingsController extends BaseController {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    IOrderRevenueSettingsService iOrderRevenueSettingsService;

    //创建社联
    @ApiOperation(value = "创建返利比例")
    @PostMapping("/create")
    @ResponseBody
    public ResponseResult<Void> createOrderRevenueSettings(OrderRevenueSettings orderRevenueSettings, HttpServletRequest request){
        Integer id =getUidFromRequest(request);
        if(id !=null) {
            iOrderRevenueSettingsService.addInfo(orderRevenueSettings,id);
            return new ResponseResult<Void>(SUCCESS,"保存成功");
        }else {
            return new ResponseResult<Void>(408,"登录过期请重新登录");
        }
    }

    @ApiOperation(value = "修改社联返利比例")
    @PostMapping("/updateOrderRevenueSettings")
    @ResponseBody
    public ResponseResult<Void> updateOrderRevenueSettings(OrderRevenueSettings orderRevenueSettings, HttpServletRequest request){
        Integer id =getUidFromRequest(request);
        if(id !=null) {
            iOrderRevenueSettingsService.updateInfo(orderRevenueSettings,id);
            return new ResponseResult<Void>(SUCCESS,"更改成功");
        }else {
            return new ResponseResult<Void>(408,"登录过期请重新登录");
        }
    }
    //查询团单详情的协会列表
    @ApiOperation(value = "查询团单详情的协会列表")
    @PostMapping("/getOrderRevenueSettings")
    @ResponseBody
    public ResponseResult<OrderRevenueSettings> getOrderRevenueSettings(HttpServletRequest request){
//        Integer id =getUidFromRequest(request);
        OrderRevenueSettings data = iOrderRevenueSettingsService.getInfo();
        return new ResponseResult<OrderRevenueSettings>(SUCCESS,data);
//        if(id !=null) {
//            OrderRevenueSettings data = iOrderRevenueSettingsService.getInfo();
//            return new ResponseResult<OrderRevenueSettings>(SUCCESS,data);
//        }else {
//            return new ResponseResult<OrderRevenueSettings >(408,"登录过期请重新登录");
//        }
    }

    private Integer getUidFromRequest(HttpServletRequest request){
        //获取headers中的参数
        String token = request.getHeader("token");
        System.err.print(token);
        if(stringRedisTemplate.opsForValue().get(token) != null) {
            return Integer.valueOf(stringRedisTemplate.opsForValue().get(token).toString());
        }else {
            return null;
        }
    }
}
